<template>
	<view>
		<u-navbar title="扫描相册" :titleStyle="{color:'#fff'}" leftIconColor="#fff" placeholder autoBack bgColor="#c1282e">
			<view class="flexright xiaobai" slot="right" v-if="!type" @click="type=!type">管理</view>
			<view class="flexright xiaobai" slot="right" v-else @click="type=!type">取消管理</view>
		</u-navbar>
		<view class="flexleft flex-wrap">
			<view class="videobox" v-for="(item,index) in list.data" :key="item.id" @click="clickimg(index,item.id)">
				<image :src="item.image_text" mode=""></image>
				<view class="videocontent" v-show="item.isgx"></view>
				<view class="checkicon" v-show="item.isgx">
					<u-icon name="checkmark-circle-fill" color="#c1282e" size="28"></u-icon>
				</view>
			</view>
		</view>
		<view class="posibottom mainpadding flexbetween ffffff" v-if="type">
			<view class="submitbtn" style="background-color: #ff0000;" @click="submitsc">删除</view>
			<view class="submitbtn margin_left" @click="submitdc" v-if="is_export">导出</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				type:false,
				page: 1,
				limit: 30,
				list: {
					data: []
				},
				checkarr:[],
				is_export:false,
			}
		},
		onLoad() {
			this.init()
			this.getshow()
		},
		watch:{
			type(newval){
				this.list.data.forEach(item=>{
					item.isgx = false
				})
				this.checkarr = []
			}
		},
		methods: {
			getshow(){
				httpRequest.request("/api/user/exportData", 'GET', {
				}, false, false, true).then(res => {
					this.is_export = res.data.is_export
				})
			},
			submitdc(){//导出pdf
				let data = []
				this.checkarr.forEach(item=>{
					data.push(item.id)
				})
				// #ifdef H5
				location.href = "https://app.shiyijia.vip/index/index/exportPdf?ids="+data.join()
				// #endif
				// #ifdef APP-PLUS
				httpRequest.request("/api/user/addUserImagesrecord", 'GET', {
					ids:data.join()
				}, false, false, true).then(res => {
					if (res.code == 1) {
						let url = "https://app.shiyijia.vip/index/index/exportPdf?ids="+data.join()
						plus.runtime.openURL(url)
					}else{
						httpRequest.toast(res.msg)
					}
				})
				
				// #endif
				
			},
			submitsc(){//删除
				let data = []
				this.checkarr.forEach(item=>{
					data.push(item.id)
				})
				httpRequest.request("/api/user/delUserImages", 'GET', {
					ids:data.join()
				}, false, false, true).then(res => {
					httpRequest.toast(res.msg)
					if (res.code == 1) {
						this.init()
					}
				})
			},
			clickimg(index,id){
				if(this.type==false){
					this.lbtpriview(index,this.list.data)
				}else{
					if(!this.list.data[index].isgx){
						this.checkarr.push(this.list.data[index])
					}else{
						let indexs = ""
						this.checkarr.forEach((item,index)=>{
							if(item.id==id){
								indexs = index
							}
						})
						if(indexs!==""){
							this.checkarr.splice(indexs,1)
						}
					}
					console.log(this.checkarr,"惊雷");
					this.list.data[index].isgx = !this.list.data[index].isgx
				}
			},
			lbtpriview(index, arr){ //预览图片
				var i = []; //获取当前页面的轮播图数据
				arr.forEach(item=>{
					i.push(item.image_text)
				})
				//uniapp预览轮播图
				uni.previewImage({
					current: index, //预览图片的下标
					urls: i //预览图片的地址，必须要数组形式，如果不是数组形式就转换成数组形式就可以
				})
			},
			init(isPage, page) {
				let _this = this;
				let data = {
					page: page || 1,
					limit: _this.limit,
				}
				let url = '/api/user/userImagesIndex'
				httpRequest.request(url, 'GET', data, false, false, true).then(res => {
					if (res.code == 1) {
						this.clws = false
						res.data.data.forEach(item => {
							this.$set(item, "isgx", false)
						})
						let resList = res.data;
						let dataList = _this.list;
						if (isPage == true) {
							_this.list.data = dataList.data.concat(resList.data);
						} else {
							this.list = res.data
							_this.list.data = resList.data;
						}
					}
				})
			},
			onReachBottom() {
				let _this = this;
				let list = _this.list;
				let page = _this.page;
				if(!list.last_page){
					return false
				}
				if (list.last_page <= page) {
					return false;
				}
				page++;
				_this.page = page;
				_this.init(true, page);
			},
		}
	}
</script>

<style lang="scss" scoped> 
.videobox {
		position: relative;
		width: 33.33%;
		height: 250rpx;
		margin-top: 5rpx;

		image {
			width: 98%;
			height: 100%;
		}

		.videocontent {
			top: 0;
			left: 0;
			position: absolute;
			background-color: rgba(255, 255, 255, .2);
			width: 100%;
			height: 100%;
		}
		.checkicon{
			top: 10rpx;
			right: 10rpx;
			position: absolute;
		}
	}
</style>
